import { ElMessage } from 'element-plus'
import { getCurrentWatcher, onWatcherCleanup } from 'vue'

export const myfetch = async (url: string, options: RequestInit) => {
  const controller = new AbortController()
  if (getCurrentWatcher()) {
    onWatcherCleanup(() => {
      ElMessage.error('取消了请求')
      controller.abort()
    })
  }

  const res = await fetch(url, {
    ...options,
    signal: controller.signal
  })

  let json: any = {}
  try {
    json = await res.json()
  } catch (error) {
    console.log('error', error)
  }
  return json
}
